//
// Created by cheng on 2021/02/02.
//

#include "MyUtil.h"

int MyUtil::RandomInt(int min, int max) {
    int res = (int)(rand() % (max - min) + min);
    return res;
}

float MyUtil::RandomFloat(float min, float max) {
    float result = rand() % 10000 * 0.0001 * (max-min) + min;
    return (float)result;
}

std::vector<int> MyUtil::GetRandomIntVector(int len, int min, int max) {
    std::vector<int> res;
    res.reserve(len);
    for (int i = 0; i < len; i++) {
        res.push_back(RandomInt(min, max));
    }
    return res;
}

void MyUtil::GetRandomIntArray(int *arr, int len, int min, int max) {
    for (int i = 0; i < len; i++) {
        arr[i] = RandomInt(min, max);
    }
}

int MyUtil::Partition(std::vector<int> &arr, int left, int right) {
    int i = left, j = right+1;
    int pos = left; // 哨兵坐标
    int posValue = arr[pos];
    while (true) {
        while (arr[++i] < posValue && i < j);
        while (arr[--j] > posValue);
        if(i > j) {
            break;
        }
        std::swap(arr[i], arr[j]);
    }
    arr[pos] = arr[j];
}
